package org.android.sss;

import org.android.tools.Global;
import org.android.tools.SSSHelper;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class SearchForProductsInShopActivity extends ListActivity {

	private int pos;
	private AlertDialog alertDialog;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Builder alertDialogBuilder = new Builder(this);
		alertDialogBuilder.setTitle("Do you want to give a plus or a minus for this product?")
		.setCancelable(true).setPositiveButton("Plus", new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				int plusProduct = SSSHelper.plusProduct(Global.products[pos].idproduct, Global.fb_uid, 1);
				String tmp = "";
				if (plusProduct == 0) {
					tmp = "You have already voted on this product";
				}
				else if (plusProduct == 1) {
					tmp = "You gave plus correctly.";
					Global.products[pos].plus += 1;
					refreshProducts();
				}
				else {
					tmp = "Some error!";
				}
				Toast.makeText(SearchForProductsInShopActivity.this, tmp, Toast.LENGTH_LONG).show();
			}
			
		}).setNegativeButton("Minus", new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				int plusProduct = SSSHelper.plusProduct(Global.products[pos].idproduct, Global.fb_uid, -1);
				String tmp = "";
				if (plusProduct == 0) {
					tmp = "You have already voted on this product";
				}
				else if (plusProduct == 1) {
					tmp = "You gave minus correctly.";
					Global.products[pos].plus -= 1;
					refreshProducts();
				}
				else {
					tmp = "Some error!";
				}
				Toast.makeText(SearchForProductsInShopActivity.this, tmp, Toast.LENGTH_LONG).show();
			}
		});
		alertDialog = alertDialogBuilder.create();

		refreshProducts();
	}

	protected void refreshProducts() {
		String[] values = new String[Global.products.length]; 
		for (int i = 0; i < Global.products.length; i++) {
			values[i] = Global.products[i].name + " " + Global.products[i].price.toString() +
					"DKK \n" +Global.products[i].brand + "\n" +
					"How packed: " + Global.products[i].howPacket + 
					"\nInfo: " + Global.products[i].info + 
					"\nPoints: "+ Global.products[i].plus.toString();

		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, values);
		setListAdapter(adapter);
		
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		String item = (String) getListAdapter().getItem(position);
		alertDialog.setMessage(item);
		pos = position;
		alertDialog.show();
	}

}
